package com.droowa.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

public class LoginCheckInterceptor implements HandlerInterceptor{

	@Override
	//view처리 종료 후에 호출
	//3.Object-Handler메소드(HandlerMethod타입)
	//4:Exception - 발생한 오류
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	//Handler(controller메소드들)처리 종료 후
	//4.ModelAndView - handler메소드가 return한 ModelAndView
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	//handler처리 전에 호출
	//return type이 boolean:false리턴 ->다음으로 이동하지 않는다.
	public boolean preHandle(HttpServletRequest request,
							HttpServletResponse response,Object handler) throws Exception {
		System.out.println("preHandle()실행");
		// 로그인 여부를 사전 체크
		HttpSession session = request.getSession();
		Object loginInfo=session.getAttribute("login_info");
		if(loginInfo==null){//로그인 X
			ModelAndView mv=new ModelAndView("customer/login_form.jsp","error_message","로그인을 먼저 하세요");
			throw new ModelAndViewDefiningException(mv);
		}
		return true;
	}

	

}
